package when3meet.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import when3meet.model.ClientInfo;
import when3meet.utilities.SpringUtilities;
import when3meet.view.CreateEventFrame;

/**
 * Deal with action when addCloseEventButton is clicked, store the choices into
 * local model
 * 
 * @author Xi
 * 
 */
public class AddCloseEventChoiceBtnController implements ActionListener {

	String[] choices;
	CloseEventRadioBtnController closeEventChoiceController;
	CreateEventFrame frame;
	public static JFrame displayChoiceFrame;

	public AddCloseEventChoiceBtnController(CloseEventRadioBtnController cecController) {
		closeEventChoiceController = cecController;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		choices = closeEventChoiceController.getChoices();

		// store choices in local model
		ClientInfo ci = ClientInfo.getInstance();
		ci.setChoices(choices);

		CloseEventRadioBtnController.disposeFrame();
		JPanel p = new JPanel(new SpringLayout());

		displayChoiceFrame = new JFrame();
		displayChoiceFrame.add(p);
		displayChoiceFrame.setVisible(true);
		int x = CreateEventFrame.getInstance().getLocation().x + CreateEventFrame.getInstance().getWidth();
		int y = CreateEventFrame.getInstance().getLocation().y;
		displayChoiceFrame.setLocation(x, y);

		for (int i = 0; i < choices.length; i++) {

			JLabel l = new JLabel("Choice " + (i + 1) + ":", JLabel.TRAILING);
			p.add(l);
			JTextField textField = new JTextField(10);
			textField.setText(choices[i]);
			textField.setEditable(false);
			l.setLabelFor(textField);
			p.add(textField);

		}

		SpringUtilities.makeCompactGrid(p, choices.length, 2, // rows, cols
				6, 6, // initX, initY
				6, 6); // xPad, yPad
		displayChoiceFrame.pack();
	}

	public static void disposeFrame() {
		if (displayChoiceFrame != null)
			displayChoiceFrame.dispose();
	}
}
